Conditions | 36 |
Paths | 3 |
Total Lines | 58 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like stringFuncs.toKeywords often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** global: UB */ |
||
27 | toKeywords: function(){ |
||
28 | var title = this; |
||
29 | /* INPUT My Product List |
||
30 | * OUTPUT product-list */ |
||
31 | |||
32 | // go thru all words |
||
33 | var words = title.trim().split(" "); |
||
34 | for (var i = 0;i<words.length;i++){ |
||
35 | |||
36 | // only lowercase |
||
37 | words[i] = words[i].toLowerCase().trim(); |
||
38 | |||
39 | // delete word if blank or junk |
||
40 | if (words[i] == "" || |
||
41 | words[i] == "," || |
||
42 | words[i] == ")" || |
||
43 | words[i] == "(" || |
||
44 | words[i] == "/" || |
||
45 | words[i] == "\\" || |
||
46 | words[i] == "&" || |
||
47 | words[i] == "\t" || |
||
48 | (words.length > 1 && |
||
49 | (words[i] == "our" || |
||
50 | words[i] == "us" || |
||
51 | words[i] == "my" || |
||
52 | words[i] == "it" || |
||
53 | words[i] == "your" || |
||
54 | words[i] == "new" || |
||
55 | words[i] == "newest" || |
||
56 | words[i] == "latest" || |
||
57 | words[i] == "and" || |
||
58 | words[i] == "or" || |
||
59 | words[i] == "the" || |
||
60 | words[i] == "best" || |
||
61 | words[i] == "all" || |
||
62 | words[i] == "in" || |
||
63 | words[i] == "on" || |
||
64 | words[i] == "out" || |
||
65 | words[i] == "top"|| |
||
66 | words[i] == "how" || |
||
67 | words[i] == "why" || |
||
68 | words[i] == "what" || |
||
69 | words[i] == "where" || |
||
70 | words[i] == "when" || |
||
71 | words[i] == "which" || |
||
72 | words[i] == "who" || |
||
73 | words[i] == "whom" |
||
74 | ) |
||
75 | ) |
||
76 | ) { |
||
77 | words.splice(i, 1); |
||
78 | i--; |
||
1 ignored issue
–
show
|
|||
79 | } |
||
80 | } |
||
81 | |||
82 | // calculate an ID from title words |
||
83 | return words.join("-"); |
||
84 | }, |
||
85 | |||
170 | UB.registerFuncs(String.prototype, stringFuncs); |